import { cloneDeep,uniqBy } from 'lodash'
import { parse } from 'qs'
import { message } from 'antd'
import { Link, hashHistory } from 'dva/router'
import {
  getHouseRentDetail,
  saveHouseRentOrUpdate
} from './forRentEditServ'

const defultState = {
  tiltle:"",
  actionType: '',
  type:null,
  detail: {},
  devices:[],
  images:[],
  outerImage:[],
  houseTypeImage:[],
  query: {
    accountId: '',
    permissionRoleIds: [],
    productIds: [],
  }
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname, query}) => {
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
          // 执行业务操作
          let {id, type, actionType} = query
          if(id){
            dispatch({type: 'getDetail', payload: {id, type, actionType}})
          }else{
            let title = ""
            if (type == 0){
              title = '市场化租房'
            } else if(type ==  1){
              title = '公租房'
            }else if(type ==  2){
              title = '保障性租赁住房'
            }else if(type == 3){
              title = '人才公寓'
            }
            dispatch({type: 'updateStore', payload: {type, title, actionType}})
          }
        }
      })
    }
  },
  
  effects: {    
    * update ({payload}, {put, select, call}) {
        let {detail} = payload
        console.log(detail)
        let {images, outerImage, houseTypeImage} = yield select(state => state[tmpModule.namespace])
        if(images && images.length > 0){
          detail.images = images.join(",")
        }
        if(outerImage && outerImage.length > 0){
          detail.outerImage = outerImage.join(",")
        }
        if(houseTypeImage && houseTypeImage.length > 0){
          detail.houseTypeImage = houseTypeImage.join(",")
        }
        if(detail.devices && detail.devices.length > 0){
          detail.devices = detail.devices.join(",")
        }
        let {data, success, msg} = yield call(saveHouseRentOrUpdate, detail)
        if (success) {
          detail = {}
          yield put({type: 'updateStore', payload: {detail}})
          if(payload.detail.id){
            message.success('编辑成功').then(() => {
              if(type == 1){
                hashHistory.push('/home/buildingAdmin/publicRent')
              }else if(type == 2){
                hashHistory.push('/home/buildingAdmin/protectRent')
              }else if(type == 3){
                hashHistory.push('/home/buildingAdmin/tenantRent')
              }
            })
          }else{
            message.success('新增成功').then(() => {
              if(type == 1){
                hashHistory.push('/home/buildingAdmin/publicRent')
              }else if(type == 2){
                hashHistory.push('/home/buildingAdmin/protectRent')
              }else if(type == 3){
                hashHistory.push('/home/buildingAdmin/tenantRent')
              }
            })
          }
          
        } else {
          message.error(msg)
        }
    },

    * getDetail ({payload}, {put, select, call}) {
      try {
        let {type, actionType} = payload
        let title = ""
        if (type == 0){
          title = '市场化租房'
        } else if(type ==  1){
          title = '公租房'
        }else if(type ==  2){
          title = '保障性租赁住房'
        }else if(type == 3){
          title = '人才公寓'
        }
        let {data, success} = yield call(getHouseRentDetail, payload)
        let detail = {}
        if (success) {
          detail = data
          let images = detail.images;
          if(images){
            images = images.split(",")
          }else{
            images = []
          }
          let houseTypeImage = detail.houseTypeImage
          if (houseTypeImage){
            houseTypeImage = houseTypeImage.split(",")
          }else{
            houseTypeImage = []
          }
          let outerImage = detail.outerImage
          if(outerImage){
            outerImage = outerImage.split(",")
          }else{
            outerImage = []
          }
          let devices = []
          if(detail.devices){
            detail.devices = detail.devices.split(",")
          }
        
          yield put({type: 'updateStore', payload: {detail, title, actionType, images, houseTypeImage, outerImage, type, devices}})
        }
      } catch (e) {
        console.error(e)
      }
    },
  },
  
  reducers: {
    updateStore (preState, action) {
      return {...preState, ...action.payload}
    }
  }
}

export default tmpModule
